﻿#include "knifegapparadialog.h"
#include "ui_knifegapparadialog.h"

KnifeGapParaDialog::KnifeGapParaDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::KnifeGapParaDialog),m_column(25),m_row(20)
{
    ui->setupUi(this);
    setWindowFlags (Qt::Window | Qt::FramelessWindowHint);
    setWindowModality(Qt::ApplicationModal);

    m_pModel = new QStandardItemModel(m_row, m_column, this);
    memset(&m_knifeGapData, 0x00, sizeof(KnifeGapPara));

    initWidget();
    initControl();

}

KnifeGapParaDialog::~KnifeGapParaDialog()
{
    delete ui;
}

void KnifeGapParaDialog::initWidget()
{

}

void KnifeGapParaDialog::initControl()
{
    //根据不同分辨率设置控件的位置和尺寸
    initResolution();
    initControlStyle();//初始化窗体控件样式
}

void KnifeGapParaDialog::initResolution()
{
    double factoryX = getFactoryX();
    double factoryY = getFactoryY();

    this->resize(GLB_SCR_WIGHT*factoryX,GLB_SCR_HEIGHT*factoryY);
    this->move((GLB_SCR_WIGHT*factoryX-this->width())/2+g_mainWidgetPos.x(),(GLB_SCR_HEIGHT*factoryY-this->height())/2+g_mainWidgetPos.y());
    ui->buttonTypeLogo->setGeometry(GLB_EDGE_WIGHT*factoryX,GLB_EDGE_HEIGHT*factoryY,GLB_LICON_SIZE*factoryX,GLB_LICON_SIZE*factoryY);
    ui->labelMainTitle->setGeometry((GLB_EDGE_WIGHT+GLB_LICON_SIZE+GLB_EDGE_WIGHT)*factoryX,GLB_EDGE_HEIGHT*factoryY,
                                    (GLB_SCR_WIGHT-(GLB_EDGE_WIGHT+GLB_LICON_SIZE+GLB_EDGE_WIGHT*2))*factoryX,GLB_TEXT_L_HEIGHT*factoryY);
    ui->labelSubTitle->setGeometry((GLB_EDGE_WIGHT+GLB_LICON_SIZE+GLB_EDGE_WIGHT+GLB_TAB_WIGHT)*factoryX,(GLB_EDGE_HEIGHT+GLB_LICON_SIZE-GLB_TEXT_HEIGHT)*factoryY,
                                   (GLB_SCR_WIGHT-(GLB_EDGE_WIGHT+GLB_LICON_SIZE+GLB_EDGE_WIGHT+GLB_TAB_WIGHT*2))*factoryX,GLB_TEXT_HEIGHT*factoryY);

    ui->frameBack->setGeometry(0*factoryX,0*factoryY,GLB_SCR_WIGHT*factoryX,GLB_SCR_HEIGHT*factoryY);
    ui->labelPageNum->setGeometry(GLB_EDGE_WIGHT*factoryX,(GLB_SCR_HEIGHT-GLB_TEXT_HEIGHT-GLB_EDGE_WIGHT)*factoryY,(GLB_SCR_WIGHT-GLB_EDGE_WIGHT*2)*factoryX,GLB_TEXT_HEIGHT*factoryY);

    ui->framePageBtn->setGeometry(0*factoryX,(GLB_SCR_HEIGHT-GLB_LBUT_HEIGHT-GLB_EDGE_WIGHT)*factoryY,GLB_SCR_WIGHT*factoryX,GLB_LBUT_HEIGHT*factoryY);
    ui->buttonPgUp->setGeometry((GLB_SCR_WIGHT-(GLB_EDGE_WIGHT+GLB_BAS_WIGHT*3+GLB_LBUT_WIGHT*4))*factoryX,0*factoryY,GLB_LBUT_WIGHT*factoryX,GLB_LBUT_HEIGHT*factoryY);
    ui->buttonPgDn->setGeometry((GLB_SCR_WIGHT-(GLB_EDGE_WIGHT+GLB_BAS_WIGHT*2+GLB_LBUT_WIGHT*3))*factoryX,0*factoryY,GLB_LBUT_WIGHT*factoryX,GLB_LBUT_HEIGHT*factoryY);
    ui->buttonOk->setGeometry((GLB_SCR_WIGHT-(GLB_EDGE_WIGHT+GLB_BAS_WIGHT*1+GLB_LBUT_WIGHT*2))*factoryX,0*factoryY,GLB_LBUT_WIGHT*factoryX,GLB_LBUT_HEIGHT*factoryY);
    ui->buttonCancel->setGeometry((GLB_SCR_WIGHT-(GLB_EDGE_WIGHT+GLB_BAS_WIGHT*0+GLB_LBUT_WIGHT*1))*factoryX,0*factoryY,GLB_LBUT_WIGHT*factoryX,GLB_LBUT_HEIGHT*factoryY);

    ui->frameSheet->setGeometry(10 *factoryX,100*factoryY,1000*factoryX,425*factoryY);
    ui->tableView->setGeometry(0,0,ui->frameSheet->width(),ui->frameSheet->height());

    //label样式
    ui->labelMainTitle->setFont(fontSize_L());
    ui->labelMainTitle->setStyleSheet(titleTextColour());
    ui->labelSubTitle->setFont(fontSize_M());
    ui->labelSubTitle->setStyleSheet(noteTextColour());
    ui->labelPageNum->setFont(fontSize_M());
    ui->labelPageNum->setStyleSheet(titleTextColour());

}

void KnifeGapParaDialog::initControlStyle()
{
    SetStyle setControlStyle;
    setControlStyle.setUiName(this->objectName());

    ui->labelPageNum->setText(tr("PageNum: 0/0"));//页数: 0/0
    ui->frameBack->setStyleSheet(transparentStyle());
    QString str = setControlStyle.getStyleSheet(this->objectName());
    ui->frameBack->setStyleSheet(str);
    //四个按钮样式表一样
    ui->buttonPgUp->setStyleSheet(confirmIconStyle());
    ui->buttonPgUp->setTopImage(setControlStyle.getSharedTopStyleSheet(ui->buttonPgUp->objectName()),12);

    ui->buttonPgDn->setStyleSheet(confirmIconStyle());
    ui->buttonPgDn->setTopImage(setControlStyle.getSharedTopStyleSheet(ui->buttonPgDn->objectName()),12);

    ui->buttonOk->setStyleSheet(confirmIconStyle());
    ui->buttonOk->setTopImage(setControlStyle.getSharedTopStyleSheet(ui->buttonOk->objectName()),12);

    //不显示前三个按钮
    ui->buttonPgUp->setVisible(false);
    ui->buttonPgDn->setVisible(false);
    ui->buttonOk->setVisible(false);

    ui->buttonCancel->setStyleSheet(confirmIconStyle());
    ui->buttonCancel->setTopImage(setControlStyle.getSharedTopStyleSheet(ui->buttonCancel->objectName()),12);

    // 设置视图的属性
    ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection);
    ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);  //触发器 点击无事件
    ui->tableView->setAlternatingRowColors(true); //交替颜色
//    ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
//    ui->tableView->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);

    // 设置QTableView的边框、网格线颜色和选择行的颜色
    ui->tableView->setStyleSheet("QTableView {"
                             "border: 1px solid #444444;" // 设置边框颜色和宽度
                             "gridline-color: #444444;"   // 设置网格线颜色
                             "}"
                             "QTableView::item:selected {"
                             "background-color: #888888;" // 设置选择项的背景色
                             "color: #ffffff;"            // 设置选择项的文字颜色
                             "}"
                             "QHeaderView::section {"
                             "background-color: #555555;" // 设置表头的背景色
                             "color: #ffffff;"            // 设置表头的文字颜色
                             "padding: 4px;"              // 设置表头内边距
                             "border: 1px solid #444444;" // 设置表头边框颜色和宽度
                             "}"
                             "QScrollBar:vertical {"
                             "width: 15px;"               // 设置垂直滚动条宽度
                             "}");

    // 填充模型
    for(int row = 0; row < m_row; row++) {
        for(int column = 0; column < m_column; column++) {
            QModelIndex index = m_pModel->index(row, column);
            m_pModel->itemFromIndex(index)->setTextAlignment(Qt::AlignCenter);//居中显示
            m_pModel->setData(index, QVariant(m_knifeGapData.GapArray[row][column]));
        }
    }

    ui->tableView->setModel(m_pModel);

}

void KnifeGapParaDialog::setTypeLogo(QString tStyle)
{
    ui->buttonTypeLogo->setUnPreBtnLogo(tStyle);
}

void KnifeGapParaDialog::setMainTitle(QString str)
{
    ui->labelMainTitle->setText(str);
}

void KnifeGapParaDialog::setSubTitle(QString str)
{
    ui->labelSubTitle->setText(str);
}

void KnifeGapParaDialog::recvData(int type, int id)
{
    //点了主板的按钮后，现在这个g_pMachine就是对应主板的
    if (type != DCMD_SEND_MOTO_GAINPARA)
        return;
    if (id != 0)
        return;

    //得到当前主板的参数
    KnifeGapPara para = g_pMachine->getGainPara();
    memcpy(&m_knifeGapData, &para, sizeof(KnifeGapPara));
    m_row = m_knifeGapData.cellx;
    m_column = m_knifeGapData.celly;

    m_pModel->setRowCount(m_row);
    m_pModel->setColumnCount(m_column);

    for(int row = 0; row < m_row; row++) {
        for(int column = 0; column < m_column; column++) {
            QModelIndex index = m_pModel->index(row, column);
            m_pModel->setData(index, QVariant(m_knifeGapData.GapArray[row][column]));
        }
    }

}

void KnifeGapParaDialog::on_buttonCancel_clicked()
{
    done(0);
}
